package com.ihealthway.all.groupbuy.modules.front;

import com.ihealthway.all.groupbuy.services.*;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.OrderInfo;
import com.ihealthway.all.groupbuy.vo.UserInfo;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

/**
 * Front personal page class
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@At("/front/personal")
@IocBean
public class ShopUserOrderModule {

    private static final Log logger = Logs.get();

    @Inject
    private CompanyService companyService;

    @Inject
    private SysDictService sysDictService;

    @Inject
    private UserInfoService userInfoService;

    @Inject
    private OrderInfoService orderInfoService;

    @Inject
    private WinnerLogService winnerLogService;

    @At
    @Ok("beetl:front/shop_user_order.jsp")
    public void home(HttpServletRequest req) {
        UserInfo user = (UserInfo) req.getSession().getAttribute(Constants.WECHAT_LOGIN_SESSION);
        req.setAttribute("user", user);
        req.setAttribute("orderList", orderInfoService.getOrderInfoForFront(user.getUserId()));
        req.setAttribute("rewardList", winnerLogService.getWinLogForFront(user.getUserId()));
    }

    @At
    @Ok("beetl:front/shop_user_register.jsp")
    public void register(@Param("re") String redirectURL, HttpServletRequest req) {
        String ui = "/front/shop/home.do";
        if (Strings.isNotBlank(redirectURL)) {
            ui = redirectURL.replace("0", "/");
            ui = ui.replace("1", ".");
            ui = ui.replace("2", "?");
            ui = ui.replace("3", "=");
        }
        logger.infof("Register create redirect url: %s", ui);
        req.setAttribute("re", ui);
        req.setAttribute("user", req.getSession().getAttribute(Constants.WECHAT_LOGIN_SESSION));
        req.setAttribute("sysDict", sysDictService.getSysDictOnFront());
        req.setAttribute("company", companyService.getCompany());
    }

    @At
    @POST
    public AjaxReturn updateUserInfo(@Param("..") UserInfo userInfo,  HttpServletRequest req) {
        logger.infof("Update user info: %s", userInfo);
        AjaxReturn ar = Ajax.ok();
        if (Strings.isNotBlank(userInfo.getArea())) {
            String[] areas = userInfo.getArea().split(" ");
            userInfo.setProvince(areas[0]);
            userInfo.setCity(areas[1]);
            userInfo.setArea(areas[2]);
        }
        ar.setOk(userInfoService.updateUserInfo(userInfo));
        if (ar.isOk()) {
            UserInfo sessionUser = (UserInfo) req.getSession().getAttribute(Constants.WECHAT_LOGIN_SESSION);
            userInfo.setOpenId(sessionUser.getOpenId());
            userInfo.setNickName(sessionUser.getNickName());
            userInfo.setHeadImg(sessionUser.getHeadImg());
            userInfo.setCountry(sessionUser.getCountry());
            userInfo.setLang(sessionUser.getLang());
            userInfo.setSubscribe(sessionUser.getSubscribe());
            userInfo.setFollowTime(sessionUser.getFollowTime());
            req.getSession().removeAttribute(Constants.WECHAT_LOGIN_SESSION);
            req.getSession().setAttribute(Constants.WECHAT_LOGIN_SESSION, userInfo);
        }
        return ar;
    }

    @At
    @POST
    public AjaxReturn updateOrderStatus(@Param("orderId") String orderId, @Param("orderStat") String orderStatus) {
        logger.infof("OrderNum[%s] update the status to[%s]", orderId, orderStatus);
        AjaxReturn ar = Ajax.ok();
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(orderId);
        orderInfo.setDataStatus(orderStatus);
        String orderNo = orderInfoService.updateOrderStatus(orderInfo);
        ar.setOk(Strings.equals(orderId, orderNo));
        return ar;
    }
}
